/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package pe.gov.mtc.dgac.licencias.action;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.actions.DispatchAction;
import pe.gov.mtc.dgac.licencias.bean.ClienteTO;
import pe.gov.mtc.dgac.licencias.bean.ClienteoperadorTO;
import pe.gov.mtc.dgac.licencias.bean.FuncionTO;
import pe.gov.mtc.dgac.licencias.bean.OperadorAereoTO;
import pe.gov.mtc.dgac.licencias.service.ClienteoperadorService;

/**
 *
 * @author externo.gamaro
 */
public class ClienteOperadorAction extends DispatchAction{
    private final static String SUCCESS = "success";
    private Log logger = LogFactory.getLog(this.getClass());
    private ClienteoperadorService cliopeService;
    
    public ClienteOperadorAction(ClienteoperadorService cliopeService) {
        super();
        this.cliopeService=cliopeService;
        
    }
    public ActionForward insertClienteOperador(ActionMapping mapping, ActionForm form, HttpServletRequest req, HttpServletResponse response) throws Exception {
        logger.debug("insertarClienteOperador");
        String forward="success";
        ActionMessages errors = new ActionMessages(); 
        String codcli = req.getParameter("cliente");
        String fecinicio= req.getParameter("txtfecinicio");
        String fectermino= req.getParameter("txtfectermino");
        String codfuncion= req.getParameter("txtfuncion");
        String codoperador= req.getParameter("txtoperador");
        
        try {
            HttpSession sesion = req.getSession();
         
               ClienteoperadorTO cliop=new ClienteoperadorTO();
               OperadorAereoTO op=new OperadorAereoTO();
               FuncionTO fnc=new FuncionTO();
               // parametros previos                             
               op.setCodOperador(Long.parseLong(codoperador));
               fnc.setCodFuncion(Long.parseLong(codfuncion));
               cliop.setCod_cliente(Long.parseLong(codcli));
               cliop.setCodFuncion(fnc);
               cliop.setOperadorAereo(op);
               cliop.setSfechaInicio(fecinicio);
               cliop.setSfechaTermino(fectermino);
                // parametros final
               String result=(String)(cliopeService.insertClienteOperador(cliop)).get("pResult");
               String msg="";
               if(result.equals("OK")){
                 cliop.setCod_cliente(((ClienteTO) sesion.getAttribute("Cliente")).getCodCliente());
                 cliop.setCodClieOper(0);
                 op.setCodOperador(0L);
                 cliop.setOperadorAereo(op);
                 List<ClienteoperadorTO> listcliop=cliopeService.buscarClienteOperador(cliop);
                 sesion.setAttribute("listclienteoperador", listcliop);
                 msg="Registro Grabado..";
               }else{
                 msg="ERR:"+result;
               }
               sesion.setAttribute("msg",msg);   
            
            
            //getServletContext().getRequestDispatcher("/licencias/ClienteOperador.jsp").forward(req, response);
            
        }
        catch (Exception e) {
           logger.error(e);
           errors.add("login", new ActionMessage("errors.interno","insertarClienteOperador"));  
	}
	 
       
              
        return mapping.findForward(forward);
        
    }  
   
    public ActionForward deleteClienteOperador(ActionMapping mapping, ActionForm form, HttpServletRequest req, HttpServletResponse response) throws Exception {
        logger.debug("eliminarClienteOperador");
        String forward="success";
        ActionMessages errors = new ActionMessages(); 
        String codcli = req.getParameter("cliente");
        String codoperador= req.getParameter("txtoperador");
        String sec = req.getParameter("txtsec");
        
        try {
            HttpSession sesion = req.getSession();
           
               ClienteoperadorTO cliop=new ClienteoperadorTO();
               OperadorAereoTO op=new OperadorAereoTO();
               op.setCodOperador(Long.parseLong(codoperador));
                // parametros previos                             
               cliop.setCod_cliente(Long.parseLong(codcli));
               cliop.setCodClieOper(Integer.parseInt(sec));
                // parametros final
               String result=(String)(cliopeService.deleteClienteOperador(cliop)).get("pResult");
               String msg="";
               if(result.equals("OK")){
                 cliop.setCod_cliente(((ClienteTO) sesion.getAttribute("Cliente")).getCodCliente());
                 cliop.setCodClieOper(0);
                 op.setCodOperador(0L);
                 cliop.setOperadorAereo(op);
                 List<ClienteoperadorTO> listcliop=cliopeService.buscarClienteOperador(cliop);
                 sesion.setAttribute("listclienteoperador", listcliop);
                 msg="Registro Eliminado..";
               }else{
                 msg="ERR:"+result;
               }
               sesion.setAttribute("msg",msg);   
           
        }
         catch (Exception e) {
           logger.error(e);
           errors.add("login", new ActionMessage("errors.interno","eliminarClienteOperador"));  
	}
             
        return mapping.findForward(forward);
        
    }  
    
    public ActionForward updateClienteOperador(ActionMapping mapping, ActionForm form, HttpServletRequest req, HttpServletResponse response) throws Exception {
       
        String forward="edit";
        logger.debug("updateClienteOperador");
        ActionMessages errors = new ActionMessages(); 
         String metodo = req.getParameter("metodo");
         String codcli = req.getParameter("cliente");
         String fecinicio= req.getParameter("txtfecinicio");
         String fectermino= req.getParameter("txtfectermino");
         String codfuncion= req.getParameter("txtfuncion");
         String codoperador= req.getParameter("txtoperador");
         String sec=req.getParameter("txtsec");
        try {
            HttpSession sesion = req.getSession();
            ClienteoperadorTO cliop=new ClienteoperadorTO();
            OperadorAereoTO op=new OperadorAereoTO();
            FuncionTO fnc=new FuncionTO();
            op.setCodOperador(Long.parseLong(codoperador));
            fnc.setCodFuncion(Long.parseLong(codfuncion));
            cliop.setCodClieOper(Integer.parseInt(sec));
            cliop.setCod_cliente(Long.parseLong(codcli));
            cliop.setCodFuncion(fnc);
            cliop.setOperadorAereo(op);
            cliop.setSfechaInicio(fecinicio);
            cliop.setSfechaTermino(fectermino);
            //
         
            if(metodo.equals("edit")){
              // System.out.println("Editando...");   
              
               // parametros final
               String result=(String)(cliopeService.updateClienteOperador(cliop)).get("pResult");
               String msg="";
               if(result.equals("OK")){
                 
                 msg="Registro Grabado..";
               }else{
                 msg="ERR:"+result;
               }
               sesion.setAttribute("msg",msg);   
            }
            
            List<ClienteoperadorTO> listcliop=cliopeService.buscarClienteOperador(cliop);
            sesion.setAttribute("clienteoperador", listcliop.get(0));
                       
        }
       catch (Exception e) {
           logger.error(e);
           errors.add("login", new ActionMessage("errors.interno","updateClienteOperador"));  
	}
        
              
        return mapping.findForward(forward);
        
    }
    public ActionForward verClienteOperador(ActionMapping mapping, ActionForm form, HttpServletRequest req, HttpServletResponse response) throws Exception {
        logger.debug("verClienteOperador");
        String forward="success";
        ActionMessages errors = new ActionMessages(); 
        String codcli = req.getParameter("cliente");
              
        try {
            HttpSession sesion = req.getSession();
         
               ClienteoperadorTO cliop=new ClienteoperadorTO();
               OperadorAereoTO op=new OperadorAereoTO(0L);
               FuncionTO fnc=new FuncionTO(0L);
               // parametros previos                             
               cliop.setCod_cliente(Long.parseLong(codcli));
               cliop.setCodFuncion(fnc);
               cliop.setOperadorAereo(op);
               // parametros final
               cliop.setCodClieOper(0);
               List<ClienteoperadorTO> listcliop=cliopeService.buscarClienteOperador(cliop);
               sesion.setAttribute("listclienteoperador", listcliop);
                                           
        }
        catch (Exception e) {
           logger.error(e);
           errors.add("login", new ActionMessage("errors.interno","verClienteOperador"));  
	}
	              
        return mapping.findForward(forward);
        
    }  
}
